﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using T.FAS.Runtime.ScheduleJob.SPI;

namespace T.FAS.Runtime.ScheduleJob.Dao
{
    public class SubscriptionNoticeDefinitionLogic
    {
        public SubscriptionNoticeSetView GetNoticeScopeSets()
        {
            var noticeSetDic = EnumUtil.EnumListDic<NoticeScope>();
            var noticeContentTypeDic = EnumUtil.EnumListDic<NoticeContentType>();
            var allSysExecuteFrequencies = new SysExecuteFrequencyDao().GetSysExecuteFrequencies();
            var sysExecuteFrequencyDic = CreateSysSubscriptionFrequencies(allSysExecuteFrequencies);
            var noticeDefinitions = new SubscriptionNoticeDefinitionDao().GetSubscriptionNoticeDefinitions(new UserDefineQueryFilter());
            var funcSets = new SysFuncSetDao().GetSysFuncSetByFuncSetByType(SysFuncSetTypeConst.NoticeScopeAuthForTeam);
            if (funcSets != null && !string.IsNullOrEmpty(funcSets.FuncSetContent))
            {
                Dictionary<string, string> userList = JsonConvert.DeserializeObject<Dictionary<string, string>>(funcSets.FuncSetContent);
                if (userList == null || !userList.ContainsKey(UserUtil.GetCurrentUserID()))
                {
                    noticeDefinitions = RemoveTeamInfo(noticeSetDic,noticeDefinitions);
                }
            }
            else
            {
                noticeDefinitions = RemoveTeamInfo(noticeSetDic, noticeDefinitions);
            }
            return new SubscriptionNoticeSetView()
            {
                SubscriptionNoticeContentType = noticeContentTypeDic,   //枚举国际化
                SubscriptionNoticeSets = noticeSetDic,                  //枚举国际化
                NoticesDefines = noticeDefinitions,                     //表数据国际化
                SysSubscriptionFrequencies = sysExecuteFrequencyDic     //表数据国际化
            };
        }
        private Dictionary<string, List<string>> CreateSysSubscriptionFrequencies(List<SysExecuteFrequency> frequencies)
        {
            var dic = new Dictionary<string, List<string>>();
            if (frequencies != null)
            {
                frequencies = frequencies.OrderBy(x=>x.SortOrder).ToList();
                var dayList = new List<string>();
                var hourList = new List<string>();
                frequencies.ForEach(x=>
                {
                    if (x.FrequencyContentDic != null)
                    {
                        if (x.FrequencyContentDic.ContainsKey(SysExecuteFrequencyConst.Frequency_Day))
                        {
                            var dayValue = x.FrequencyContentDic[SysExecuteFrequencyConst.Frequency_Day];
                            if (!dayList.Contains(dayValue))
                                dayList.Add(dayValue);
                            var hourValue = x.FrequencyContentDic[SysExecuteFrequencyConst.Frequency_Hour];
                            if (!hourList.Contains(hourValue))
                                hourList.Add(hourValue);
                        }
                    }
                });
                dic.Add(SysExecuteFrequencyConst.Frequency_Day,dayList);
                dic.Add(SysExecuteFrequencyConst.Frequency_Hour,hourList);
            }
            return dic;
        }

        private List<SubscriptionNoticeDefinition> RemoveTeamInfo(Dictionary<object,object> dic, List<SubscriptionNoticeDefinition> noticeDefinitions)
        {
            dic.Remove((int)NoticeScope.Team);
            if (noticeDefinitions != null && noticeDefinitions.Count > 0)
            {
                return noticeDefinitions.Where(x=>x.NoticeScope!= NoticeScope.Team).ToList();
            }
            return noticeDefinitions;

        }
    }
}
